﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OrderTest;

namespace FormTest
{        

    public partial class Form1 : Form
    { 

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.DataBindings.Add("Items", pubClass.service, "orderList");
            if(pubClass.service.getOrderList() != null)
            {
                foreach(Order order in pubClass.service.getOrderList())
                    listBox1.Items.Add(order.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Order order = new Order();
            List<OrderDetails> details = new List<OrderDetails>();
            if (this.textBox1.Text != "" && pubClass.service.searchOrder(order, this.textBox1.Text))
            {

                int index = listBox1.Items.IndexOf(this.textBox1.Text);
                listBox1.TopIndex = index;
                listBox1.SelectedIndex = index;
                listBox2.Items.Clear();
                details = order.getDetails();
                foreach (OrderDetails detail in details)
                    listBox2.Items.Add(detail.ToString());
                this.textBox1.Text = "已找到订单";
            }
            else
                this.textBox1.Text = "未找到订单";
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox2.Items.Clear();
            string str = listBox1.SelectedItem.ToString();
            Order ord = new Order();
            if(str.Length != 0)
            {
                foreach(Order order in pubClass.service.getOrderList())
                {
                    if (str == order.ToString())
                        ord = order;
                }
                foreach(OrderDetails detail in ord.getDetails())
                {
                    listBox2.Items.Add(detail.ToString());
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string str = listBox1.SelectedItem.ToString();
            Order ord = new Order();
            if (str.Length != 0)
            {
                foreach(Order order in pubClass.service.getOrderList())
                {
                    if (str == order.ToString())
                        ord = order;
                }
                pubClass.service.deleteOrder(ord.getOrderID());
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Order[] order = new Order[pubClass.service.getOrderList().Count];
            pubClass.service.Import(order);
            pubClass.service = new OrderService(order);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            pubClass.service.Export();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string str = listBox1.SelectedItem.ToString();
            Order ord = new Order();
            if (str.Length != 0)
            {
                foreach (Order order in pubClass.service.getOrderList())
                {
                    if (str == order.ToString())
                        ord = order;
                }
            }
            Form3 f = new Form3(ord);
            f.ShowDialog();
        }
    }
    public static class pubClass
    {
        public static OrderService service = new OrderService();
    }
}
